using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIHeroList : UIBaseWindow
    {
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIHeroList),
            layer = EUILayer.Normal,
            isFullScreen = true,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify
        protected partial class UIB_UIHeroList
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExButton btn_Recycle { protected set; get; }
            public GameObject topLeftBack { protected set; get; }
            public GameObject toggleFilter { protected set; get; }
            public GameObject empty { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textEmpty { protected set; get; }
            public UnityEngine.UI.ExLoopVerticalScrollRect loopScrollRect { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btn_Recycle", out var __tbv0);
                this.btn_Recycle = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("topLeftBack", out var __tbv1);
                this.topLeftBack = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("toggleFilter", out var __tbv2);
                this.toggleFilter = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("empty", out var __tbv3);
                this.empty = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textEmpty", out var __tbv4);
                this.textEmpty = __tbv4;
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopVerticalScrollRect>("loopScrollRect", out var __tbv5);
                this.loopScrollRect = __tbv5;
            }

            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        ListScrollAdapter<UIN_HeroListFilter> tabs = null;
        LoopScrollAdapter<UIN_HeroListCell> loopCells = null;

        int indexSelectedTab = -1;
        List<HeroData> dataList = new();

        protected UIB_UIHeroList ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIHeroList();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.tabs = this.InitListScroll<UIN_HeroListFilter>(this.ui.toggleFilter, this.OnTabChanged);
            this.tabs.InitUnitSelected(UnitSelectType.ByExToggle, this.OnTabSelected);
            this.loopCells = this.InitLoopScroll<UIN_HeroListCell>(this.ui.loopScrollRect, this.OnHeroChanged);

            this.ui.empty.SetActive(false);
            var topLeftBack = this.InitChildNode<UIN_TopLeftBack>(this.ui.topLeftBack);
            topLeftBack.Show();
        }

        protected override void OnShow(UIBaseData data)
        {
            var hData = (UD_HeroList)data;
            this.indexSelectedTab = hData.tabIndex;
            this.ViewTab();
        }

        protected override void OnHide()
        {

        }

        void ViewTab()
        {
            int cnt = (int)EM_HeroConst.MaxLength;
            int indexSelected = this.indexSelectedTab;
            if (indexSelected == -1)
                indexSelected = 0;
            this.tabs.RefillCells(cnt, indexSelected);
        }

        void OnTabChanged(UIN_HeroListFilter item, int index)
        {
            item.Show(index);
        }

        void OnTabSelected(int index, UIN_HeroListFilter item)
        {
            this.indexSelectedTab = index;
            this.ViewRightList(item);
        }

        void ViewRightList(UIN_HeroListFilter tab = null)
        {
            if (tab == null)
                tab = this.tabs.GetTEntity(this.indexSelectedTab);
            int tabIndex = tab.tabIndex;
            this.dataList.Clear();
            HeroModule.Instance.GetHeros(tabIndex, this.dataList, this.SortListFunc);

            int cnt = this.dataList.Count;
            this.loopCells.RefillCells(cnt, 0);

            this.ui.empty.SetActive(cnt == 0);
        }

        int SortListFunc(HeroData a, HeroData b)
        {
            if (a.isGeted != b.isGeted)
                return a.isGeted ? -1 : 1;
            if (a.level != b.level)
                return a.level > b.level ? -1 : 1;
            if (a.exp != b.exp)
                return a.exp > b.exp ? -1 : 1;
            if (a.star != b.star)
                return a.star > b.star ? -1 : 1;
            return a.cid < b.cid ? -1 : 1;
        }

        void OnHeroChanged(UIN_HeroListCell item, int index)
        {
            var data = this.dataList[index];
            item.Show(data);
        }

        protected override UIBaseData OnGetLeaveData()
        {
            var data = ClassPool<UD_HeroList>.Get();
            data.tabIndex = this.indexSelectedTab;
            return data;
        }

        protected override void OnDispose()
        {

        }
    }
}
